﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Enterprise.Infrastructure;
using Enterprise.Model.Meeting;
using Enterprise.Service.Meeting;
using Enterprise.Model.User;
using Enterprise.Service.User;
using Enterprise.Model.Department;
using Enterprise.Service.Department;
namespace Enterprise.UI.Web.Modules.Meeting
{
    public partial class Show : System.Web.UI.Page
    {
        int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string type = (string)Utility.sink("type", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void OnStart()
        {
            if (Id == 0)
                return;

            MeetingService mService = new MeetingService();
            MeetingRecevieService mrService = new MeetingRecevieService();
            var q = mService.MeetingDisp(Id);
            if (q != null)
            {
                List<MeetingRecevieEntity> olr = mrService.MeetingRecevieList().Where(p => p.MID == Id).ToList();
                if (type == "ok")
                {
                    StringBuilder sb = new StringBuilder();
                    DepartmentService dService = new DepartmentService();
                    List<DepartmentEntity> list = dService.DepartmentList();
                    sb.Append("<table class=\"htmlgrid\" width=\"100%\">");
                    foreach (DepartmentEntity dept in list)
                    {
                        if (GetUserList(dept.DEPTID, olr).Count > 0)
                        {
                            sb.Append("<tr class=\"datarow\">");
                            sb.AppendFormat("<td width=\"100\" align=\"left\">{0}</td>", dept.DNAME);
                            sb.AppendFormat("<td align=\"left\">{0}</td>", GetUsers(dept.DEPTID, olr));
                            sb.Append("</tr>");
                        }
                    }
                    sb.Append("</table>");
                    lb.Text = sb.ToString();
                }
                else if (type == "no")
                {
                    StringBuilder sb = new StringBuilder();
                    //取得总的需要签收的人员
                    List<MeetingRecevieEntity> noread = new List<MeetingRecevieEntity>();
                    if (!string.IsNullOrEmpty(q.MUsers))
                    {
                        string[] arr = q.MUsers.Split(',');
                        string[] arrok = new string[olr.Count];
                        for (int i = 0; i < olr.Count; i++)
                        {
                            arrok[i] = olr[i].MRUSERID.ToString();
                        }
                        //在两个数组中找出不同元素
                        var query = arr.Except(arrok).ToList(); ;
                        foreach (var var in query)
                        {
                            sb.AppendFormat("{0}  ", UserService.GetUserName(int.Parse(var)));
                        }
                    }
                    else
                    {
                        sb.Append("无签收范围");
                    }
                    lb.Text = sb.ToString();
                }
            }

        }
        /// <summary>
        /// 返回用户姓名
        /// </summary>
        /// <param name="deptId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        private string GetUsers(int deptId, List<MeetingRecevieEntity> list)
        {
            StringBuilder sb = new StringBuilder();
            UserService uService = new UserService();
            List<UserEntity> listUser = uService.UserList().Where(p => p.DEPTID == deptId).ToList();
            var q = from p in listUser
                    join pp in list on p.USERID equals pp.MRUSERID
                    select p;
            foreach (var var in q)
            {
                sb.AppendFormat("{0}  ", var.UNAME);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 返回用户列表
        /// </summary>
        /// <param name="deptId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        private List<UserEntity> GetUserList(int deptId, List<MeetingRecevieEntity> list)
        {
            UserService uService = new UserService();
            List<UserEntity> listUser = uService.UserList().Where(p => p.DEPTID == deptId).ToList();
            var q = from p in listUser
                    join pp in list on p.USERID equals pp.MRUSERID
                    select p;
            return q.ToList();
        }

    }
}